package com.itheima.reggie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.db.Session;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    HttpSession session;


    //员工登录
    @Override
    public Employee login(String username, String password) {
        //根据username查找数据库中用户信息
        Employee employee = employeeMapper.findByUsername(username);
        //判断用户是否存在
        if(employee == null){
            throw new CustomException("此用户不存在！");
            //return ResultInfo.error("此用户不存在！");
        }

        // 3.比较密码
        //3-1 将用户明文转为密文
        String md5 = SecureUtil.md5(password);

        if(!StrUtil.equals(md5,employee.getPassword())){
            throw new CustomException("密码错误");
            //return ResultInfo.error("密码错误");
        }

        //判断员工状态
        if(employee.getStatus() != 1){
            throw new CustomException("此用户被禁用,请联系管理员");
            //return ResultInfo.error("此用户被禁用,请联系管理员");
        }
        //登录成功，存入session信息
        session.setAttribute(Constant.SESSION_EMPLOYEE,employee);
        //返回成功结果
        return employee;
    }

    @Override
    public List<Employee> find(String name) {
        List<Employee> list =  employeeMapper.find(name);
        return list;
    }


    //添加员工
    @Override
    public void add(Employee employee) {

        //雪花算法设置id
//        long lid = IdUtil.createSnowflake(1, 1).nextId();
//        employee.setId(lid);
        //加密密码
        employee.setPassword(SecureUtil.md5(employee.getPassword()));
        //设置员工状态
        employee.setStatus(Employee.STATUS_ENABLE);
        /*//设置创建时间
        employee.setCreateTime(new Date());
        //设置修改时间
        employee.setUpdateTime(new Date());
        //设置创建人id
        Employee e = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setCreateUser(e.getId());
        //设置修改人id
        employee.setUpdateUser(e.getId());*/

        //employeeMapper.save(employee);
        employeeMapper.insert(employee);
    }

    //回显
    @Override
    public Employee findById(Long id) {
        Employee emp = employeeMapper.findById(id);
        return emp;
    }

    //修改
    public void update(Employee employee) {
        //设置操作者id
       /* Employee e = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        employee.setUpdateUser(e.getId());
        //设置修改时间
        employee.setUpdateTime(new Date());*/
        //调用mapper的sql语句
        //employeeMapper.update(employee);


        employeeMapper.updateById(employee);
    }
}
